package com.lin.exceptionHandler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    public ModelAndView runtimeExceptionHandler(RuntimeException e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/static/accountException.jsp");
        modelAndView.addObject("msg", e.getMessage()); // 回写错误信息
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody // 指明返回Json数据, 否则会将返回值作为视图名称返回 --导致找不到视图404
    public Result ExceptionHandler(Exception e) {
        return new Result(500, e.getMessage(), null);
    }
}
